/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kallsonys.session;

import com.kallsonys.dao.CustomerDAO;
import com.kallsonys.object.Customer;
import com.kallsonys.object.ElementQuery;
import com.kallsonys.object.ws.ListElement;
import com.kallsonys.service.ElementListPage;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.sql.DataSource;

/**
 *
 * @author alladiab
 */
@Stateless
public class CustomerFacade extends AbstractFacade<Customer> {

    @PersistenceContext(unitName = "Kallsonys-WarPU")
    private EntityManager em;
    
    private com.kallsonys.dao.CustomerDAO dao;
    @Resource(mappedName = "jdbc/OMS", type = DataSource.class)
    DataSource dsOMS;

    @PostConstruct
    void postConstruct() {
        dao = new CustomerDAO(dsOMS);
    } 

    protected EntityManager getEntityManager() {
        return em;
    }

    public CustomerFacade() {
        super(Customer.class);
    }

    /**
     * Crea el Cliente
     * @param customer
     * @return 
     */
    public long CreateCustomer(com.kallsonys.object.Customer customer) throws Exception {
        try {
            customer.setCustomeremail(customer.getCustomeremail().toLowerCase());
            this.create(customer);
            return customer.getCustomerid();
        } catch (Exception e) 
        {
             e.printStackTrace();
             throw new Exception(e);
        }

    }
    
    public boolean UpdateCustomer(com.kallsonys.object.Customer customer) throws Exception {
        try {
            customer.setCustomeremail(customer.getCustomeremail().toLowerCase());
            this.UpdateCustomer(customer);
            return true;
        } catch (Exception e) 
        {
             e.printStackTrace();
             throw new Exception(e);
        }

    }

    public ElementQuery<Customer> getAll(int page) {
        ElementQuery<Customer> objReturn = new ElementQuery<Customer>();
        List<Customer> p = new ArrayList<Customer>();
        int total = 0;

        Query queryfindByProductidproduct = em.createNamedQuery("Customer.findAll");

        int[] rango = {page * 10, (page * 10 + 10)};
        p = (List<Customer>) this.findRange(rango, queryfindByProductidproduct);
        total = this.count();

        objReturn.setLstItems(p);
        objReturn.setTotalPages(total);

        return objReturn;
    }

    public Customer getByCustomerdocument(String document) {
        Customer c = null;
        if (document != null && document.length() > 0) {
            Query queryfindByProductidproduct = em.createNamedQuery("Customer.findByCustomerdocument");
            queryfindByProductidproduct.setParameter("customerdocument", document);

            List<Customer> lstC = (List<Customer>) queryfindByProductidproduct.getResultList();

            if (!lstC.isEmpty()) {
                c = lstC.get(0);
            }
        }

        return c;
    }

    public Customer getByCustomerid(long customerid) {
        Customer c = null;

        Query queryfindByProductidproduct = em.createNamedQuery("Customer.findByCustomerid");
        queryfindByProductidproduct.setParameter("customerid", customerid);
        List<Customer> lstC = (List<Customer>) queryfindByProductidproduct.getResultList();

        if (!lstC.isEmpty()) {
            c = lstC.get(0);
        }

        return c;
    }
    
    /**
     * getByEmail
     * @param email
     * @return 
     */
    public Customer getByEmail(String email) {
        Customer c = null;

        Query queryfindByProductidproduct = em.createNamedQuery("Customer.findByCustomeremail");
        queryfindByProductidproduct.setParameter("customeremail", email);
        List<Customer> lstC = (List<Customer>) queryfindByProductidproduct.getResultList();

        if (!lstC.isEmpty()) {
            c = lstC.get(0);
        }

        return c;
    }
    
    
    public String validateCustomer(String email, String password) {
        
        String retorno = "ok";
        
        //Busco X Email
        Customer c = getByEmail(email.toLowerCase());
        
        if ( c != null )
        {
            if ( !c.getCustomerpassword().equals(password))
            {
                retorno = "Clave Invalida";
            }
        }
        else
        {
            retorno = "Usuario No Existe";
        }
        
        return retorno;
    }
    
    
    /**
     * Retorna la Lista de Categorias
     * @return 
     */
    public List<ListElement> getClientCategorys()
    {
        List<ListElement> lstItems = new ArrayList<ListElement>();
        
        lstItems.add(new ListElement("0", "Platino"));
        lstItems.add(new ListElement("1", "Dorado"));
        lstItems.add(new ListElement("2", "Plateado"));
        
        return lstItems;
    }
    
    
    public ElementListPage<com.kallsonys.service.Customer> getCustomersByBilling(Date startDate, Date endDate, int page) throws Exception
    {
        ElementListPage<com.kallsonys.service.Customer> retorno = dao.getCustomersByBilling(startDate, endDate, page);
        return retorno;
    }
    
    public ElementListPage<com.kallsonys.service.Customer> getCustomersByProduct(long productId, int page) throws Exception  {
        ElementListPage<com.kallsonys.service.Customer> retorno = dao.getCustomersByProduct(productId, page);
        return retorno;
    }
    
    
    public ElementListPage<com.kallsonys.service.Customer> getCustomersByDocument(long id, String document) throws Exception  {
        ElementListPage<com.kallsonys.service.Customer> retorno = new ElementListPage<com.kallsonys.service.Customer>();
        List<com.kallsonys.service.Customer> lstCustomer = new ArrayList<com.kallsonys.service.Customer>();
        
        if ( id == -1 )
        {
             com.kallsonys.service.Customer cus = dao.getByCustomer(document, 1);
             lstCustomer.add(cus);
             
             retorno.setPages(1);
             retorno.setElementList(lstCustomer);
        }
        else
        {
            com.kallsonys.service.Customer cus = dao.getByCustomer(id);
             lstCustomer.add(cus);
             
             retorno.setPages(1);
             retorno.setElementList(lstCustomer);
        }
        
        return retorno;
    }
}
